﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using Dao.Interface;
using NHibernate.Criterion;
using NHibernate;

namespace Dao
{
    public class PageRepository : Repository<Page>, IPageRepository
    {
        /// <summary>
        /// 获取页的分页集合
        /// </summary>
        /// <param name="areaID">分区ID</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="sortField">排序字段</param>
        /// <returns></returns>
        public IQueryable<Page> LoadAllByPage(int areaID, int pageIndex, int pageSize, out int pageCount, string sortType)
        {
            var pageList = this.LoadAll().Where(i => i.Area.ID == areaID && i.MarkDelete == false);
            int totalCount = pageList.Count();
            if (totalCount % pageSize == 0)
            {
                pageCount = totalCount / pageSize;
            }
            else
            {
                pageCount = totalCount / pageSize + 1;
            }

            if (sortType == "CreateTime")
            {
                pageList = pageList.OrderByDescending(o => o.CreateTime);
            }
            else
            {
                pageList = pageList.OrderByDescending(o => o.UpdateTime);
            }
            pageList = pageList.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return pageList;
        }

        /// <summary>
        /// 获取共享页的分页集合
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageCount">总页数</param>
        /// <returns></returns>
        public IQueryable<Page> LoadAllSharePageByPage(int pageIndex, int pageSize, out int pageCount)
        {
            var pageList = this.LoadAll().Where(i => i.MarkDelete == false && i.Share == true);
            int totalCount = pageList.Count();
            if (totalCount % pageSize == 0)
            {
                pageCount = totalCount / pageSize;
            }
            else
            {
                pageCount = totalCount / pageSize + 1;
            }
            pageList = pageList.OrderByDescending(o => o.CreateTime);
            pageList = pageList.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return pageList;
        }

        /// <summary>
        /// 获取页集合
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="delete">是否删除</param>
        /// <returns></returns>
        public IList<Page> LoadAllPage(object userID, bool delete)
        {
            return this.Session.CreateCriteria(typeof(Page))
                .Add(Restrictions.Eq("MarkDelete", delete))
                .CreateCriteria("Area")
                .CreateCriteria("NoteBook")
                .CreateCriteria("UserInfo").Add(Restrictions.Eq("ID", userID))
                .List<Page>();
        }


    }
}
